package com.echo.register.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.echo.register.client.cmn.DictFeignClient;
import com.echo.register.enums.DictEnum;
import com.echo.register.model.user.Patient;
import com.echo.register.user.mapper.PatientMapper;
import com.echo.register.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: PatientServiceImpl
 * @Package: com.echo.register.user.service.impl
 * @Description:
 * @Author 谢同学
 * @Create 2024/1/2 12:15
 * @Version 1.0
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {
    @Autowired
    private DictFeignClient dictFeignClient;

    /**
     * 获取就诊人列表
     *
     * @param userId 用户id
     * @return 就诊人信息
     */
    @Override
    public List<Patient> findAllUserId(Long userId) {
        List<Patient> patients = baseMapper.selectList(new LambdaQueryWrapper<Patient>().eq(Patient::getUserId, userId));
        // 远程调用dict，得到编码对应的含
        patients.forEach(this::packPatient);
        return patients;
    }

    /**
     * 根据id获取就诊人信息
     * @param id 就诊人id
     * @return 就诊人信息
     */
    @Override
    public Patient getPatientId(Long id) {
        Patient patient = baseMapper.selectById(id);
        this.packPatient(patient);
        return patient;
    }

    /**
     * 就诊人信息处理
     * @param patient 就诊人信息
     */
    private void packPatient(Patient patient) {
        if (patient == null) {
            return;
        }
        //根据证件类型编码，获取证件类型具体
        String contactsCertificatesTypeString = null;
        String certificatesTypeString = null;
        if (patient.getContactsCertificatesType() != null && patient.getCertificatesType() != null) {
            certificatesTypeString = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType()); // 证件类型
            contactsCertificatesTypeString = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType()); // 联系人证件类型
        }
        // 省
        String provinceString = null;
        if (patient.getProvinceCode() != null) {
            provinceString = dictFeignClient.getName(patient.getProvinceCode());
        }
        // 市
        String cityString = null;
        if (patient.getCityCode() != null) {
            cityString = dictFeignClient.getName(patient.getCityCode());
        }
        // 区
        String districtString = null;
        if (patient.getDistrictCode() != null) {
            districtString = dictFeignClient.getName(patient.getDistrictCode());
        }

        // 封装信息
        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);

        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", (provinceString == null ? "" : provinceString) + (cityString == null ? "" : cityString) + (districtString == null ? "" : districtString) + (patient.getAddress() == null ? "" : patient.getAddress()));
    }
}
